import { _decorator, Camera, Component, Node, v2, RigidBody2D, ERigidBody2DType, Vec2, BoxCollider2D } from 'cc';
import { createUINode } from '../../utils';
import { BackGround } from '../backGround/backGround';
import { PlayerManager } from '../player/playerManager';
import { JoyStickManager } from '../joyStick/joyStickManager';
import DataManager from '../runTime/dataManager';
import { TileMap01 } from '../backGround/tileMap01';
const { ccclass, property } = _decorator;

@ccclass('BattleManager')
export class BattleManager extends Component {
    _stage: Node;


    onLoad(): void {
        // this._stage.    
    }

    start(): void {
        this.generateStage(); // 创建舞台
        this.init();
    }

    generateStage(): void {
        this._stage = createUINode("stage"); // 创建空节点
        
        // 添加钢体
        const rigidbody = this._stage.addComponent(RigidBody2D);
        rigidbody.type = ERigidBody2DType.Static;

        // 添加形状      
        this.addBox(this._stage, v2(-1050, 0), 100, 3000);
        this.addBox(this._stage, v2(1050, 0), 100, 3000);
        this.addBox(this._stage, v2(0, -1550), 2200, 100);
        this.addBox(this._stage, v2(0, 1550), 2200, 100);

        this._stage.setParent(this.node);
    }
    

    addBox(bg: Node, offset: Vec2, width: number, height: number) {
        const box = bg.addComponent(BoxCollider2D);
        box.size.width = width;
        box.size.height = height;
        box.offset = offset;
    }

    async init() {
        await this.generateBackGround();
        this.generateJoystick();
        this.generatePlayer();

        console.log(this._stage.children);
        console.log(this.node.children);
        
        
    }

    async generateBackGround() {
        const bg = createUINode("background");
        const backGround = bg.addComponent(TileMap01);
        await backGround.init();
        bg.setParent(this._stage);
    }
    

    async generatePlayer() {
        const player = createUINode("palyer");
        const playerManaer = player.addComponent(PlayerManager);
        playerManaer.init();
        player.setParent(this._stage);
    }

    async generateJoystick() {
        const stick = createUINode("joyStick");
        const joyStick = stick.addComponent(JoyStickManager);
        joyStick.init();
        stick.setParent(this._stage);
        DataManager.Instance._joyStick = joyStick;
    }
}


